from fastapi import APIRouter
from fastapi.responses import StreamingResponse
import pandas as pd
import plotly.express as px
from APP.common.middleware.vanna_api.userVanna import userVanna
import json,re
import plotly.graph_objects as go
import plotly.figure_factory as ff
import asyncio
from APP.config.log_conf import log
from APP.app.dbgpt.schemas.chat_data_schema import ChatDataQueryStr
from APP.app.dbgpt.services.chat_data_service import ChatDataService


chatdata_router = APIRouter()


@chatdata_router.post("/plotly_charts",
                      summary='生成plotly图表',
                      description='调用dbgpt接口,根据data和chat生成plotly图表,包含表格和echarts图表',
                      response_description='table:plotly组装的表格,content:大模型回复内容,sql:大模型生成的sql,echarts:vanna生成的plotly图表'
                      )
async def plotly_charts(request: ChatDataQueryStr):

    message = request.query
    
    # 调用dbgpt接口,获取大模型回复字典
    response_data = await ChatDataService.request_dbgpt(message=message,model='deepseek-chat',chat_param='dbgpt_test',temperature=0.3)
    #流式返回数据，包含sql、table、echarts、content
    return StreamingResponse(ChatDataService.generate_stream_response(message=message,response_data=response_data), media_type="text/event-stream")